# 作者；吴同学
# 2025年 01月 01日 22时 33分 25秒
# 1207570802@qq.com
# 1、练习封装案例（和上课保持一致即可）
# 2、练习私有属性和私有方法（和上课保持一致即可）
class Person:
    def __init__(self, name, gender, work, add):
        self.__name = name
        self.__gender = gender
        self.work = work
        self.add = add

    def get_name(self):
        return self.__name

    def set_name(self, new_name):
        self.__name = new_name

    def get_gender(self):
        return self.__gender

    def set_gender(self, new_gender):
        self.__gender = new_gender

    def person(self):
        return f"姓名: {self.__name}, 性别: {self.__gender}, 工作: {self.work}, 地址: {self.add}"

    def __judge_work(self):
        if self.work == 'stu':
            print('好好进步')
        elif self.work == 'teacher':
            print('好好生活')

    def judge(self):
        self.__judge_work()

# 3、练习单继承，多重继承案例（和上课保持一致即可）


class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} speak")


class Pet:
    def __init__(self, master):
        self.master = master


class Dog(Animal, Pet):
    def __init__(self, name, master, color):
        Animal.__init__(self, name)
        Pet.__init__(self, master)
        self.color = color

    def speak(self):
        print(f"{self.color} 的 {self.name} barks")

    def who_master(self):
        print(f"{self.color} 的 {self.name} 的主人是 {self.master}")

# 4、实现单例模式（和上课保持一致即可）


def singleton(cls):
    instance = {}  # 用一个字典来保存已经创建的实例

    # 返回一个新的函数，替换原来的类
    def get_instance(*args, **kwargs):
        if cls not in instance:  # 检查类是否已经有实例
            instance[cls] = cls(*args, **kwargs)  # 创建并保存实例
        return instance[cls]  # 返回已创建的实例
    return get_instance  # 返回包装过的函数


@singleton
class Singleton:
    def __init__(self):
        print("Singleton instance created")
# 5、通过try进行异常捕捉，确保输入的内容一定是一个整型数，
# 然后判断该整型数是否是对称数，12321就是对称数，
# 123321也是对称数，否则就打印不是，非整型抛异常，不是对称数抛异常


def is_palindrome(num):
    # 通过字符串处理，反转后与原字符串是否相等判断是否是回文数
    return str(num) == str(num)[::-1]


def abnormal_exam():
    try:
        num = int(input("请输入一个整数: "))

        if is_palindrome(num):
            print(f"{num} is palindrome")
        else:
            raise ValueError(f"{num} is not palindrome")
    except ValueError as e:
        print(f"输入错误: {e}")
    except Exception as e:
        print(f"出错了: {e}")


if __name__ == '__main__':
    # per1 = Person('小吴', '男', 'stu', 'GuangZhou')
    # print(per1.get_name())
    # print(per1.get_gender())
    # print(per1.work)
    # print(per1.add)
    # per1.set_name('小蔡')
    # per1.set_gender('女')
    # print(per1.get_name())
    # print(per1.get_gender())
    # print(per1.person())
    # per1.judge()

    # dog = Dog('大黄', '小吴', '黄色')
    # dog.speak()
    # dog.who_master()

    # singleton1 = Singleton()
    # singleton2 = Singleton()
    #
    # print(singleton1 is singleton2)
    abnormal_exam()
